﻿using System.Drawing;
using System.Windows.Forms;

namespace PlusPlusPaint.Layers.VisualObjects
{
    class TextArea : DynamicallyGeneratedVisualObject
    {
        #region member variables
        string text = "";
        readonly Font defaultFont = new Font(FontFamily.GenericSansSerif, 20);
        #endregion

        #region properties
        public override bool AcceptsMostText
        {
            get
            {
                return true;
            }
        }

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                this.RecalculateSize();
            }
        }

        protected override string ObjectTypeName
        {
            get
            {
                return "TextArea";
            }
        }
        #endregion

        #region constructors
        public TextArea()
        {

        }

        public TextArea(Point p)
        {
            this.Location = p;
        }
        #endregion

        #region overrides
        public override SerializedObject Serialize()
        {
            SerializedObject so = base.Serialize();
            so.NamedStrings["text"] = Text; // No implicit bool->int, what?
            return so;
        }

        public override void Deserialize(SerializedObject s)
        {
            base.Deserialize(s);
            Text = s.NamedStrings["text"];
        }

        public override bool PositionIsInHitbox(Point p)
        {
            return FilledRectangle.IsInRectangleBoundedBy(ClientRectangle, p);
        }

        public override IVisualObject Clone()
        {
            TextArea ta = new TextArea(this.Location);
            ta.Text = this.Text;
            return ta;
        }

        public override void BurnOn(Image im)
        {
            using (Brush br = new SolidBrush(this.Colour))
                Graphics.FromImage(im).DrawString(Text, defaultFont, br, this.Location);
        }
        #endregion

        #region public methods
        public void PutChar(char c)
        {
            this.Text += c;
        }
        #endregion

        #region private methods
        private void RecalculateSize()
        {
             this.Size = TextRenderer.MeasureText(Text, defaultFont);
        }
        #endregion
    }
}
